package com.wuye.web.fee.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuye.utils.Result;
import com.wuye.web.fee.entity.FeeWater;
import com.wuye.web.fee.entity.FeeWaterParam;
import com.wuye.web.fee.service.IFeeWaterService;
import com.wuye.web.system.entity.LiveHouse;
import com.wuye.web.system.service.ILiveHouseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 水费表 前端控制器
 * </p>
 *
 * @author 孤筏
 * @since 2023-08-02
 */
@RestController
@RequestMapping("/api/feeWater")
public class FeeWaterController {
    @Resource
    private IFeeWaterService feeWaterService;
    @Resource
    private ILiveHouseService liveHouseService;

    @ApiOperation("水费列表")
    @GetMapping("/list")
    public Result getList(FeeWaterParam param){
        IPage<FeeWater> page = feeWaterService.getList(param);
        return Result.success(page);
    }

    @ApiOperation("新增水费信息")
    @PostMapping
    public Result addFeeWater(@RequestBody FeeWater feeWater){
        QueryWrapper<LiveHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LiveHouse::getHouseId,feeWater.getHouseId())
                            .eq(LiveHouse::getUseStatus,1);
        LiveHouse liveHouse = liveHouseService.getOne(queryWrapper);
        if (Objects.isNull(liveHouse)){
            return Result.error(500,"该房间暂无人使用，不可添加水费信息");
        }
        feeWater.setUserId(feeWater.getUserId());
        feeWaterService.saveFeeWater(feeWater);
        return Result.success();
    }

    @ApiOperation("编辑水费信息")
    @PutMapping
    public Result editFeeWater(@RequestBody FeeWater feeWater){
        QueryWrapper<LiveHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LiveHouse::getHouseId,feeWater.getHouseId())
                .eq(LiveHouse::getUseStatus,1);
        LiveHouse liveHouse = liveHouseService.getOne(queryWrapper);
        if (Objects.isNull(liveHouse)){
            Result.error(500,"该房间暂无人使用，不可编辑水费信息");
        }
        feeWater.setUserId(liveHouse.getUserId());
        feeWaterService.updateFeeWater(feeWater);
        return Result.success();
    }

    @ApiOperation("删除水费")
    @DeleteMapping("/{waterId}")
    public Result deleteFeeWater(@PathVariable("waterId") Integer waterId){
        QueryWrapper<FeeWater> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FeeWater::getWaterId,waterId)
                .eq(FeeWater::getPayWaterStatus,"1");
        FeeWater one = feeWaterService.getOne(queryWrapper);
        if(one!=null){
            return Result.error(500,"已缴费，不能删除");
        }
        //删除操作
        boolean remove = feeWaterService.removeById(waterId);
        if(remove){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }

    @ApiOperation("缴费")
    @PostMapping("/payWater")
    public Result payWater(@RequestBody FeeWater feeWater){
        boolean b = feeWaterService.updateById(feeWater);
        if(b){
            return Result.success();
        }
        return Result.error(500,"缴费失败");
    }
}